﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam
{
    public partial class FormMain : Form
    {
        int NhanVienID;
        NhanVien nv;
        CDuLieu data;
        public FormMain(int NVID)
        {
            InitializeComponent();
            NhanVienID = NVID;
        }

        private void quảnLýTácGiảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTacgia f = new FormTacgia();
            f.MdiParent = this;
            f.Show();
        }

        private void quảnLýThểLoạiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormQuanlytheloai f = new FormQuanlytheloai();
            f.MdiParent = this;
            f.Show();
        }

        private void quảnLýNhàXuấtBảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormNXB f = new FormNXB();
            f.MdiParent = this;
            f.Show();
        }

        private void quảnLýThôngTinNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormNhacungcap f = new FormNhacungcap();
            f.MdiParent = this;
            f.Show();
        }

        private void quảnLýThôngTinSáchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormQuanlythongtinsach f = new FormQuanlythongtinsach();
            f.Show();
        }

        private void quảnLýPhiếuNhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormPhieuNhap f = new FormPhieuNhap();
            f.Show();
        }

        private void quảnLýPhiếuĐặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormPhieuDat f = new FormPhieuDat();
            f.Show();
        }

        private void quảnLýPhiếuĐềNghịXuấtKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormPhieuDeNghiXuatKho f = new FormPhieuDeNghiXuatKho();
            f.Show();
        }

        private void lậpHóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormBanHang f = new FormBanHang();
            f.Show();
        }

        private void quảnLýHóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormQuanLyHoaDon f = new FormQuanLyHoaDon();
            f.Show();
        }

        private void xemSáchTồnKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormThongKeSach f = new FormThongKeSach();
            f.Show();
        }

        private void xemSáchTrênKệToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormThongKe f = new FormThongKe();
            f.Show();
        }

        private void quảnLýPhiếuXuấtKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormPhieuXuat f = new FormPhieuXuat();
            f.Show();
        }

        private void quảnlýphânLoạiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormPhanLoai f = new FormPhanLoai(nv);
            f.Show();
        }

        private void TạotàikhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void đổiMậtKhẩuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormDoiMatKhau f = new FormDoiMatKhau(nv);
            f.Show();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            data = CDuLieu.khoitao();
            nv = data.TimNhanVien(NhanVienID);
            foreach (ToolStripMenuItem item in menuStrip1.Items)
            {
                ExplorerItem(item);
            }
        }

        private void ExplorerItem(ToolStripMenuItem item)
        {
            if (KiemTraQuyen(item.Name)==false)
            {
                item.Visible = false;
                return;
            }
            if (item.DropDownItems.Count > 0)
            {               
                for (int i = 0; i < item.DropDownItems.Count; i++)
                {
                    if (item.DropDownItems[i] is ToolStripMenuItem)
                    {
                        ExplorerItem((ToolStripMenuItem)item.DropDownItems[i]);
                    }
                }
            }
        }

        //Kiểm tra quyền để load lên giao diện
        public bool KiemTraQuyen(string name)
        {
            bool kt = false;
            foreach (PhanQuyen plq in nv.Role.PhanQuyens)
            {
                if (plq.Quyen.QuyenNo == name)
                {
                    kt = true;
                    break;
                }
            }
            return kt;
        }
    }
}
